param(
	$invoiceWebYearUrl = "$(Read-Host 'Enter the invoice web url for the processing year [e.g. http://sites/invoices/2012]')"
)

$invoiceDocumentName = "InvoiceDocuments"
$invoiceList = "Invoices"

Write-Host "`n===================`n# Deleting Invoices #`n===================" -ForegroundColor Yellow

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
#Load the site
Write-Host  "Loading Site " $invoiceWebYearUrl -ForegroundColor Green
$web = Get-SPWeb -identity $invoiceWebYearUrl # "http://turnersdms/invoices/2011/"
Write-Host  "Site Loaded " -ForegroundColor Green

#Load the document library
Write-Host  "Loading Invoice Document Library" 
$list = $web.Lists[$invoiceDocumentName]
$spFolder = $web.GetFolder($invoiceDocumentName)
$spFileCollection = $spFolder.Files
Write-Host "Document library loaded" -ForegroundColor Green

$listItems = $list.Items;
$count = $listItems.Count - 1

for($intIndex = $count; $intIndex -gt -1; $intIndex--)
{
	#"Deleting: " + $intIndex
	$listItems.Delete($intIndex);
}

$folderCount = $list.RootFolder.SubFolders.Count
for($intIndex = $folderCount-1; $intIndex -gt 0; $intIndex--)
{
	Write-Host "Deleting folder number $intIndex"
	$list.RootFolder.SubFolders.Delete("$intIndex")
}

$listInvoices = $web.Lists[$invoiceList]
$invoiceItems = $listInvoices.Items;
$invoiceItemsCount = $invoiceItems.Count - 1

Write-Host "Deleting list items"
for($intIndex = $invoiceItemsCount; $intIndex -gt -1; $intIndex--)
{
	$invoiceItems.Delete($intIndex);
}

$listInvoicesFolders = $listInvoices.Folders
$listInvoicesFoldersCount = $listInvoicesFolders.Count - 1
Write-Host "Deleting folders"
for($intIndex = $listInvoicesFoldersCount; $intIndex -gt -1; $intIndex--)
{
	$listInvoicesFolders.Delete($intIndex)
}

Write-Host "Delete Completed"

Remove-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
